/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.compose.material3

import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.testutils.assertAgainstGolden
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.test.captureToImage
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.unit.LayoutDirection
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.filters.SdkSuppress
import androidx.test.screenshot.AndroidXScreenshotTestRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@LargeTest
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 35, maxSdkVersion = 35)
class AlertDialogScreenshotTest {

    @get:Rule val composeTestRule = createComposeRule()

    @get:Rule val screenshotRule = AndroidXScreenshotTestRule(GOLDEN_MATERIAL3)

    @Test
    fun alertDialog_lightTheme() {
        composeTestRule.setMaterialContent(lightColorScheme()) {
            AlertDialog(
                onDismissRequest = {},
                title = { Text(text = "Title") },
                text = {
                    Text(
                        "This area typically contains the supportive text " +
                            "which presents the details regarding the Dialog's purpose."
                    )
                },
                confirmButton = { Button(onClick = { /* doSomething() */ }) { Text("Confirm") } },
                dismissButton = {
                    OutlinedButton(onClick = { /* doSomething() */ }) { Text("Dismiss") }
                },
            )
        }

        assertAlertDialogAgainstGolden(goldenIdentifier = "alertDialog_lightTheme")
    }

    @Test
    fun alertDialog_darkTheme() {
        composeTestRule.setMaterialContent(darkColorScheme()) {
            AlertDialog(
                onDismissRequest = {},
                title = { Text(text = "Title") },
                text = {
                    Text(
                        "This area typically contains the supportive text " +
                            "which presents the details regarding the Dialog's purpose."
                    )
                },
                confirmButton = { Button(onClick = { /* doSomething() */ }) { Text("Confirm") } },
                dismissButton = {
                    OutlinedButton(onClick = { /* doSomething() */ }) { Text("Dismiss") }
                },
            )
        }

        assertAlertDialogAgainstGolden(goldenIdentifier = "alertDialog_darkTheme")
    }

    @Test
    fun alertDialog_withVerticalButtons_lightTheme() {
        composeTestRule.setMaterialContent(lightColorScheme()) {
            AlertDialog(
                onDismissRequest = {},
                title = { Text(text = "Title") },
                text = {
                    Text(
                        "This area typically contains the supportive text " +
                            "which presents the details regarding the Dialog's purpose."
                    )
                },
                confirmButton = {
                    Button(
                        onClick = { /* doSomething() */ },
                        modifier = Modifier.width(DialogMaxWidth * 0.8f),
                    ) {
                        Text("Confirm")
                    }
                },
                dismissButton = {
                    OutlinedButton(
                        onClick = { /* doSomething() */ },
                        modifier = Modifier.width(DialogMaxWidth * 0.8f),
                    ) {
                        Text("Dismiss")
                    }
                },
            )
        }

        assertAlertDialogAgainstGolden(
            goldenIdentifier = "alertDialog_withVerticalButtons_lightTheme"
        )
    }

    @Test
    fun alertDialog_withIcon_lightTheme() {
        composeTestRule.setMaterialContent(lightColorScheme()) {
            AlertDialog(
                onDismissRequest = {},
                icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
                title = { Text(text = "Title") },
                text = {
                    Text(
                        "This area typically contains the supportive text " +
                            "which presents the details regarding the Dialog's purpose."
                    )
                },
                confirmButton = { Button(onClick = { /* doSomething() */ }) { Text("Confirm") } },
                dismissButton = {
                    OutlinedButton(onClick = { /* doSomething() */ }) { Text("Dismiss") }
                },
            )
        }

        assertAlertDialogAgainstGolden(goldenIdentifier = "alertDialog_withIcon_lightTheme")
    }

    @Test
    fun alertDialog_withIcon_darkTheme() {
        composeTestRule.setMaterialContent(darkColorScheme()) {
            AlertDialog(
                onDismissRequest = {},
                icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
                title = { Text(text = "Title") },
                text = {
                    Text(
                        "This area typically contains the supportive text " +
                            "which presents the details regarding the Dialog's purpose."
                    )
                },
                confirmButton = { Button(onClick = { /* doSomething() */ }) { Text("Confirm") } },
                dismissButton = {
                    OutlinedButton(onClick = { /* doSomething() */ }) { Text("Dismiss") }
                },
            )
        }

        assertAlertDialogAgainstGolden(goldenIdentifier = "alertDialog_withIcon_darkTheme")
    }

    @Test
    fun alertDialog_rtl() {
        composeTestRule.setContent {
            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
                AlertDialog(
                    onDismissRequest = {},
                    title = { Text(text = "Title") },
                    text = {
                        Text(
                            "This area typically contains the supportive text " +
                                "which presents the details regarding the Dialog's purpose."
                        )
                    },
                    confirmButton = {
                        Button(onClick = { /* doSomething() */ }) { Text("Confirm") }
                    },
                    dismissButton = {
                        OutlinedButton(onClick = { /* doSomething() */ }) { Text("Dismiss") }
                    },
                )
            }
        }
        assertAlertDialogAgainstGolden(goldenIdentifier = "alertDialog_rtl")
    }

    private fun assertAlertDialogAgainstGolden(goldenIdentifier: String) {
        composeTestRule
            .onNode(isDialog())
            .captureToImage()
            .assertAgainstGolden(screenshotRule, goldenIdentifier)
    }
}
